using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;

namespace WinFormsApp3
{
    public static class FileAssociationHelper
    {
        private static readonly string[] SupportedExtensions = { ".zip", ".rar", ".7z", ".tar", ".gz", ".tar.gz", ".tgz" };
        
        public static bool AreFileAssociationsRegistered()
        {
            try
            {
                // Check if at least one extension is registered to our application
                string exePath = Process.GetCurrentProcess().MainModule.FileName;
                foreach (string extension in SupportedExtensions)
                {
                    string progId = "CooZip" + extension.Replace(".", "");
                    using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Classes\" + progId + @"\shell\open\command"))
                    {
                        if (key != null)
                        {
                            string value = key.GetValue("") as string;
                            if (!string.IsNullOrEmpty(value) && value.Contains(exePath))
                            {
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
        
        public static void RegisterFileAssociations()
        {
            try
            {
                // Get the path of the current executable
                string exePath = Process.GetCurrentProcess().MainModule.FileName;
                
                foreach (string extension in SupportedExtensions)
                {
                    RegisterExtension(extension, "CooZip" + extension.Replace(".", ""), "CooZip Archive", exePath);
                }
                
                // Notify the system about the changes
                SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
                
                System.Windows.MessageBox.Show("文件关联设置成功！", "CooZip", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show($"设置文件关联失败: {ex.Message}\n\n请尝试以管理员身份运行应用程序。", 
                    "CooZip", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
        }
        
        public static void RemoveFileAssociations()
        {
            try
            {
                foreach (string extension in SupportedExtensions)
                {
                    string progId = "CooZip" + extension.Replace(".", "");
                    
                    // Remove extension association
                    using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Classes\" + extension, true))
                    {
                        if (key != null)
                        {
                            string currentProgId = key.GetValue("") as string;
                            if (currentProgId == progId)
                            {
                                key.DeleteValue("", false);
                            }
                        }
                    }
                    
                    // Remove progid keys
                    Registry.CurrentUser.DeleteSubKeyTree(@"Software\Classes\" + progId, false);
                }
                
                // Notify the system about the changes
                SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
                
                System.Windows.MessageBox.Show("文件关联已移除！", "CooZip", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show($"移除文件关联失败: {ex.Message}\n\n请尝试以管理员身份运行应用程序。", 
                    "CooZip", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
        }
        
        private static void RegisterExtension(string extension, string progId, string description, string application)
        {
            // Create extension key
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + extension))
            {
                key.SetValue("", progId);
            }
            
            // Create progid key
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + progId))
            {
                key.SetValue("", description);
            }
            
            // Create icon
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + progId + @"\DefaultIcon"))
            {
                key.SetValue("", $"\"{application}\",0");
            }
            
            // Create command
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + progId + @"\shell\open\command"))
            {
                key.SetValue("", $"\"{application}\" \"%1\"");
            }
        }
        
        [System.Runtime.InteropServices.DllImport("shell32.dll")]
        private static extern void SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);
    }
}